//
//  ImagePicViewController.m
//  BTRIP
//
//  Created by johnbk on 15/11/21.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "ImagePicViewController.h"
#import "ImagePicCollectionViewCell.h"
#import "DetailSpecialSubModel.h"
#import "SpecialDetailViewController.h"
@interface ImagePicViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property(nonatomic, retain) UIAlertController *alertViewController;
@end

@implementation ImagePicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	self.view.backgroundColor = [UIColor blackColor];
	self.automaticallyAdjustsScrollViewInsets = NO;
	self.arrayOfImageString = [NSMutableArray array];
	for (DetailSpecialSubModel *detailSpecialSubModel in self.detailSpecialModel.article_sections) {
		if (![detailSpecialSubModel.image_url isEqualToString:@""]) {
			[self.arrayOfImageString addObject:detailSpecialSubModel.image_url];
		}
	}
	[self createCollectionView];
	self.currentPosition = [self.arrayOfImageString indexOfObject:self.currentImageString];
	
	self.collectionView.contentOffset = CGPointMake(self.currentPosition * self.view.frame.size.width, 0);
	
	[self createButtonOfSaveToSystemAlbum];
}
-(void)dealloc{
	[_collectionView release];
	[_detailSpecialModel release];
	[_arrayOfImageString release];
	[_currentImageString release];
	[_buttonOfSaveToSystemAlbum release];
	[_alertViewController release];
	[super dealloc];
}
- (void)createCollectionView{
	UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
	[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
	flowLayout.minimumLineSpacing = 0;
	flowLayout.minimumInteritemSpacing = 0;
	self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:flowLayout];
	[self.view addSubview:self.collectionView];
	[self.collectionView release];
	self.collectionView.delegate = self;
	self.collectionView.dataSource = self;
	self.collectionView.backgroundColor = [UIColor whiteColor];
	self.collectionView.delegate = self;
	self.collectionView.dataSource = self;
	[self.collectionView registerClass:[ImagePicCollectionViewCell class] forCellWithReuseIdentifier:@"ImagePicCollectionViewCellIdentifier"];
	self.collectionView.bounces = NO;
	self.collectionView.pagingEnabled = YES;
	self.collectionView.showsHorizontalScrollIndicator = NO;
	
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
	ImagePicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImagePicCollectionViewCellIdentifier" forIndexPath:indexPath];
	cell.backgroundColor = [UIColor blackColor];
	cell.imageString = self.arrayOfImageString[indexPath.row];
	return cell;
	
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
	return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
	return self.arrayOfImageString.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
	return CGSizeMake(self.view.frame.size.width, self.collectionView.frame.size.height);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
	return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)createButtonOfSaveToSystemAlbum{
	self.buttonOfSaveToSystemAlbum = [UIButton buttonWithType:UIButtonTypeCustom];
	self.buttonOfSaveToSystemAlbum.frame = CGRectMake(self.view.frame.size.width - 80, 35, 20, 20);
	
	[self.buttonOfSaveToSystemAlbum setBackgroundImage:[UIImage imageNamed:@"iconfont-download"] forState:UIControlStateNormal];
	[self.view addSubview:self.buttonOfSaveToSystemAlbum];
	
	[self.buttonOfSaveToSystemAlbum addTarget:self action:@selector(saveToSystemAlbum:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveToSystemAlbum: (UIButton *)button{
	NSString *currentString = self.arrayOfImageString[(NSInteger)(self.collectionView.contentOffset.x / self.view.frame.size.width)];
	UIImage *currentImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:currentString]]];
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"确认保存??" preferredStyle:UIAlertControllerStyleAlert];
	UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
		UIImageWriteToSavedPhotosAlbum(currentImage, self, @selector(imageWriteToSavedPhotosAlbum:Error:contextinfo:), nil);
		[self.buttonOfSaveToSystemAlbum setBackgroundImage:[UIImage imageNamed:@"iconfont-download副本"] forState:UIControlStateNormal];
		
	}];
	UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"no" style:UIAlertActionStyleCancel handler:nil];
	[alert addAction:noAction];
	[alert addAction:yesAction];
	[self presentViewController:alert animated:YES completion:nil];
}
- (void)imageWriteToSavedPhotosAlbum:(UIImage *)image Error:(NSError *)error contextinfo:(NSString *)info{
	if (!error) {
		self.alertViewController = [UIAlertController alertControllerWithTitle:@"" message:@"成功" preferredStyle:UIAlertControllerStyleAlert];
		[UIView animateWithDuration:13 animations:^{
			[self presentViewController:self.alertViewController animated:YES completion:^{
				[self dismissViewControllerAnimated:self.alertViewController completion:nil];
			}];
		}];
		
	}
	else{
		NSLog(@"失败");
	}
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
	[self.navigationController.navigationBar setHidden:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
	;
	[self.delegate currentImageString:self.arrayOfImageString[(NSInteger)(self.collectionView.contentOffset.x / self.view.frame.size.width)]];
	[self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
	[self.buttonOfSaveToSystemAlbum setBackgroundImage:[UIImage imageNamed:@"iconfont-download"] forState:UIControlStateNormal];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
